<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Cache;

class Manage extends Common
{
	public function uploadFile()
	{
		adminUploadFile($_FILES);
	}

	/**
	 * 分类列表
	 * */
	public function typeList()
	{
		$pid = Request()->param('pid') ?? 0;

		$where['pid'] = $pid;

		$list = Db::name('type')->where($where)->paginate();
		$this->assign('list', $list);

		$this->assign('title', '分类列表');

		return $this->fetch();
	}

	/**
	 * 新增/编辑 分类
	 * */
	public function doType()
	{
		if (Request()->isPost()) {
			$input = Request()->param();

			$id = $input['id'];
			$pid = $input['pid'];
			$name = $input['name'];
			$is_nav = $input['is_nav'];
			$sort = $input['sort'];

			$data = [
				'pid' => $pid,
				'name' => $name,
				'is_nav' => $is_nav,
				'sort' => $sort
			];

			if ($id) {
				$res = Db::name('type')->where(['id' => $id])->update($data);
				$msg = '编辑';
			} else {
				$res = Db::name('type')->insert($data);
				$msg = '新增';
			}

			if($res){
				Cache::clear();

				adminReturn(1, $msg . '成功', url('typeList', ['pid' => $pid]));
			}else{
				adminReturn(2, $msg . '失败');
			}
		} else {
			$id = Request()->param('id') ?? 0;

			if ($id) {
				$info = Db::name('type')->where(['id' => $id])->find();
				$this->assign('info', $info);

				$this->assign('title', '编辑分类');
			} else {
				$this->assign('title', '新增分类');
			}

			return $this->fetch();
		}
	}

	/**
	 * 删除分类
	 * */
	public function delType()
	{
		$id = Request()->param('id');

		if ($id) {
			$res = Db::name('type')->where(['id' => $id])->delete();
			if ($res) {
				Cache::clear();

				adminReturn(1, '删除菜单成功', url('typeList'));
			} else {
				adminReturn(2, '删除菜单失败');
			}
		} else {
			adminReturn(2, '菜单ID缺失');
		}
	}

	/**
	 * 文章列表
	 * */
	public function articleList()
	{
		$input = Request()->param();

		$one_type_id = $input['one_type_id'];
		$two_type_id = $input['two_type_id'];
		$name = $input['name'] ?? '';
		$is_banner = $input['is_banner'];

		$where = ' 1 = 1 ';
		if($one_type_id) $where .= ' and one_type_id = ' . $one_type_id;
		if($two_type_id) $where .= ' and two_type_id = ' . $two_type_id;
		if($name) $where .= ' and title like "%'. $name .'%"';
		if($is_banner > -1) $where .= ' and is_banner = ' . $is_banner;

		$list = Db::name('article')->where($where)->paginate();
		$this->assign('list', $list);

		$type_list = Db::name('type')->where(['pid' => 0])->order('sort asc')->select();
		$this->assign('type_list', $type_list);

		if($one_type_id){
			$two_type_list = Db::name('type')->where(['pid' => $one_type_id])->order('sort asc')->select();
			$this->assign('two_type_list', $two_type_list);
		}

		$this->assign('title', '文章列表');
		return $this->fetch();
	}

	/**
	 * 获取二级分类
	 * */
	public function getTwoTypeList()
	{
		$pid = Request()->param('pid');

		$type_list = Db::name('type')->where(['pid' => $pid])->order('sort asc')->select();
		if($type_list){
			apiReturn(1, '获取数据成功', $type_list);
		}else{
			apiReturn(2, '暂无更多数据');
		}
	}

	/**
	 * 新增/编辑 文章
	 * */
	public function doArticle()
	{
		if(Request()->isPost()){
			$input = Request()->param();

			$id = $input['id'];
			$title = $input['title'];
			$one_type_id = $input['one_type_id'];
			$two_type_id = $input['two_type_id'];
			$is_banner = $input['is_banner'];
			$img = $input['img'] ?? 0;
			$content = $input['content'];
			$sort = $input['sort'] ?? 0;
			$file_name = $input['file_name'];
			$file_path = $input['file_path'];


			if(empty($title)) adminReturn(2, '请输入标题');
			if(empty($one_type_id)) adminReturn(2, '请选择分类');
			if($is_banner == 1 && empty($img)) adminReturn(2, '请上传封面图');
			if(empty($content)) adminReturn(2, '请输入文章详情');
			if($one_type_id == 55 && (empty($file_name) || empty($file_path))) adminReturn(2, '请上传文件');

			$data = [
				'title' => $title,
				'one_type_id' => $one_type_id,
				'two_type_id' => $two_type_id,
				'is_banner' => $is_banner,
				'img' => $img,
				'content' => $content,
				'sort' => $sort
			];

			if($one_type_id == 55){
				$data['file_name'] = $file_name;
				$data['file_path'] = $file_path;
			}

			if($id){
				$data['update_time'] = time();
				$res = Db::name('article')->where(['id' => $id])->update($data);
				if($res){
					adminReturn(1, '编辑成功', url('articleList'));
				}else{
					adminReturn(2, '编辑失败');
				}
			}else{
				$data['create_time'] = time();
				$res = Db::name('article')->insert($data);
				if($res){
					adminReturn(1, '新增成功', url('articleList'));
				}else{
					adminReturn(2, '新增失败');
				}
			}
		}else{
			$id = Request()->param('id');

			$type_list = Db::name('type')->where(['pid' => 0])->order('sort asc')->select();
			$this->assign('type_list', $type_list);

			if($id){
				$info = Db::name('article')->where(['id' => $id])->find();
				$info['img_path'] = getPicture($info['img']);
				$this->assign('info', $info);

				if($info['one_type_id'] > 0){
					$two_type_list = Db::name('type')->where(['pid' => $info['one_type_id']])->order('sort asc')->select();
				}else{
					$two_type_list = [];
				}
				$this->assign('two_type_list', $two_type_list);

				$this->assign('title', '编辑文章');
			}else{
				$this->assign('title', '新增文章');
			}
			return $this->fetch();
		}
	}

	/**
	 * 删除文章
	 * */
	public function delArticle()
	{
		$id = Request()->param('id');

		if ($id) {
			$res = Db::name('article')->where(['id' => $id])->delete();
			if ($res) {
				adminReturn(1, '删除文章成功', url('articleList'));
			} else {
				adminReturn(2, '删除文章失败');
			}
		} else {
			adminReturn(2, '文章ID缺失');
		}
	}

	/**
	 * 职称列表
	 * */
	public function jobList()
	{
		$list = Db::name('job')->paginate();
		$this->assign('list', $list);

		$this->assign('title', '职称列表');

		return $this->fetch();
	}

	/**
	 * 新增/编辑 职称
	 * */
	public function doJob()
	{
		if(Request()->isPost()){
			$input = Request()->param();

			$id = $input['id'];
			$name = $input['name'];
			$no = $input['no'];
			$sex = $input['sex'];
			$province_name = $input['province_name'];
			$ratify_date = $input['ratify_date'];
			$number = $input['number'];
			$job_name = $input['job_name'];
			$job_level = $input['job_level'];
			$send_company = $input['send_company'];

			if(empty($name)) adminReturn(2, '请输入姓名');
			if(empty($no)) adminReturn(2, '请输入证书编号');
			if(empty($sex)) adminReturn(2, '请选择性别');
			if(empty($province_name)) adminReturn(2, '请选择出生地点');
			if(empty($ratify_date)) adminReturn(2, '请选择批准日期');
			if(empty($number)) adminReturn(2, '请输入证件号码');
			if(empty($job_name)) adminReturn(2, '请输入专业名称');
			if(empty($job_level)) adminReturn(2, '请输入专业等级');
			if(empty($send_company)) adminReturn(2, '请输入发证单位');

			if($id) $id_count_where['id'] = ['neq', $id];

			$no_count = Db::name('job')->where(['no' => $no])->where($id_count_where)->count();
			if($no_count) adminReturn(2, '证书编号已存在');

			$number_count = Db::name('job')->where(['number' => $number])->where($id_count_where)->count();
			if($number_count) adminReturn(2, '证件号码已存在');


			$data = [
				'name' => $name,
				'no' => $no,
				'sex' => $sex,
				'province_name' => $province_name,
				'ratify_date' => $ratify_date,
				'number' => $number,
				'job_name' => $job_name,
				'job_level' => $job_level,
				'send_company' => $send_company
			];

			if($id){
				$info = Db::name('job')->where(['id' => $id])->find();
				if(!$info['qr']){
					$qr = createQr(getHost() . url('index/index/mobileJobDetail', array('id' => $id)));
					$data['qr'] = $qr;
				}
				$res = Db::name('job')->where(['id' => $id])->update($data);
				if($res){
					adminReturn(1, '编辑成功', url('jobList'));
				}else{
					adminReturn(2, '编辑失败');
				}
			}else{
				$res = Db::name('job')->insertGetId($data);
				if($res){
					$qr = createQr(getHost() . url('index/index/mobileJobDetail', array('id' => $res)));
					Db::name('job')->where(['id' => $res])->setField('qr', $qr);

					adminReturn(1, '新增成功', url('jobList'));
				}else{
					adminReturn(2, '新增失败');
				}
			}
		}else{
			$id = Request()->param('id');

			$province_list = Db::name('province')->select();
			$this->assign('province_list', $province_list);

			if($id){
				$info = Db::name('job')->where(['id' => $id])->find();
				$this->assign('info', $info);

				$this->assign('title', '编辑职称');
			}else{
				$this->assign('no', createNo());

				$this->assign('title', '新增职称');
			}
			return $this->fetch();
		}
	}

	/**
	 * 删除职称
	 * */
	public function delJob()
	{
		$id = Request()->param('id');

		if ($id) {
			$res = Db::name('job')->where(['id' => $id])->delete();
			if ($res) {
				adminReturn(1, '删除职称成功', url('jobList'));
			} else {
				adminReturn(2, '删除职称失败');
			}
		} else {
			adminReturn(2, '职称ID缺失');
		}
	}

	/**
	 * 证书编号生成规则
	 * */
	public function noRule()
	{
		$input = Request()->param();

		$id = $input['id'];
		$no_header = $input['no_header'];
		$no_length = $input['no_length'];

		if($id){
			if($no_header && $no_length <= strlen($no_header)) adminReturn(2, '证书长度不可小于等于编号开头长度');
			if($no_header && $no_length <= 0) adminReturn(2, '证书长度规则设置错误');

			$data = array(
				'no_header' => $no_header,
				'no_length' => $no_length
			);

			$count = Db::name('no_rule')->count();
			if($count > 0){
				$res = Db::name('no_rule')->where(['id' => 1])->update($data);
			}else{
				$res = Db::name('no_rule')->insert($data);
			}

			if($res){
				adminReturn(1, '保存成功');
			}else{
				adminReturn(2, '保存失败或内容未修改');
			}
		}else{
			$info = Db::name('no_rule')->where(['id' => 1])->find();
			$this->assign('info', $info);
			return $this->fetch();
		}
	}

	public function userList()
	{
		$list = Db::name('menu')->paginate(2);
		$this->assign('list', $list);

		$this->assign('title', '列表示例');

		return $this->fetch();
	}

	public function memberList()
	{
		$this->assign('title', '表单示例');

		return $this->fetch();
	}

	public function bannerList()
	{
		$this->assign('title', '轮播图');

		return $this->fetch();
	}

	public function chart()
	{
		$this->assign('title', '统计图');

		return $this->fetch();
	}
}
